package com.controller;

import com.entity.*;
import com.service.*;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName YudingController
 * @Description 预定模块控制层
 */
@Controller
public class YudingController extends BaseController{

    @Autowired
    private YudingService yudingService;

    @Autowired
    private ZuoweiService zuoweiService;
    @Autowired
    private FangshiService fangshiService;
    @Autowired
    private UsersService usersService;



    //前台-预定列表页
    @RequestMapping("/yudingManaQian")
    public ModelAndView yudingManaQian(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingManaQian");
        mav.addObject("actionName","yudingManaQian");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-预定图片列表页
    @RequestMapping("/yudingManaQiantp")
    public ModelAndView yudingManaQiantp(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingManaQiantp");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-预定管理列表页
    @RequestMapping("/yudingMana")
    public ModelAndView yudingMana(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingMana");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //后台-预定查看列表页
    @RequestMapping("/yudingChakan")
    public ModelAndView yudingChakan(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingChakan");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-预定按id列排序列表页
    @RequestMapping("/yudingManaQianSort")
    public ModelAndView yudingManaQianSort(Yuding  req, @RequestParam(defaultValue = "1", value = "pageNo") int pageNo) {
        ModelAndView mav = new ModelAndView("yudingManaQian");
        mav.addObject("actionName","yudingManaQianSort");
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-预定详情页
    @GetMapping("/yudingDetailQian/{id}")
    @ResponseBody
    public ModelAndView yudingDetailQian(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("yudingDetailQian");
        Yuding info= yudingService.findById(id);
        mav.addObject("yuding", info);

        return mav;
    }

    //后台-预定详情页
    @GetMapping("/yudingDetail/{id}")
    @ResponseBody
    public ModelAndView yudingDetail(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("yudingDetail");
        Yuding info= yudingService.findById(id);
        mav.addObject("yuding", info);
        return mav;
    }

    //获取所有预定数据接口
    @RequestMapping("/yudingAll")
    @ResponseBody
    public ApiResponse<Yuding> yudingAll(){
        List<Yuding> yudingList=yudingService.findAll();
        return ApiResponse.success(yudingList);
    }

    //根据条件（字符类型模糊匹配查询）获取预定数据接口
    @RequestMapping("/yudingSearch")
    @ResponseBody
    public ApiResponse<Yuding> yudingSearch(Yuding req){
        List<Yuding> yudingList=yudingService.find(req);
        return ApiResponse.success(yudingList);
    }

    //根据条件（字符类型完全匹配查询）获取预定数据接口
    @RequestMapping("/yudingSearchByEqualTo")
    @ResponseBody
    public ApiResponse<Yuding> yudingSearchByEqualTo(Yuding req){
        List<Yuding> yudingList=yudingService.findByEqualTo(req);
        return ApiResponse.success(yudingList);
    }

    //预定添加或修改接口
    @PostMapping("/yudingAddOrUpdate")
    @ResponseBody
    public ApiResponse<Integer> yudingAddOrUpdate(@RequestBody Yuding req, HttpSession session, Boolean isUPdateSession){



        int result = yudingService.saveOrUpdate(req);


        return ApiResponse.success(result);
    }

    //预定修改单独一个字段接口
    @PostMapping("/yudingSet")
    @ResponseBody
    public ApiResponse<Integer> yudingSet(Yuding req){
        int result = yudingService.saveOrUpdate(req);
        return ApiResponse.success(result);
    }



    //预定删除接口
    @RequestMapping("/yudingDel/{id}")
    @ResponseBody
    public ApiResponse<Integer> yudingDel(@PathVariable Integer id){
        int result = yudingService.deleteById(id);
        return ApiResponse.success(result);
    }

    //后台-预定修改页
    @RequestMapping("/yudingGet/{id}")
    @ResponseBody
    public ModelAndView yudingGet(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView("yudingSet");
        Yuding info = yudingService.findById(id);
        mav.addObject("yuding", info);
        return mav;
    }



    //后台-我的预定查看页面
    @RequestMapping("/yudingChakanMy")
    public ModelAndView yudingChakanMy(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("yudingChakanMy");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }

    //后台-我的预定管理列表页面
    @RequestMapping("/yudingManaMy")
    public ModelAndView yudingManaMy(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("yudingManaMy");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }

    //前台-我的预定管理列表页面
    @RequestMapping("/yudingManaMyQian")
    public ModelAndView yudingManaMyQian(Yuding req, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo, HttpSession session){
        ModelAndView mav = new ModelAndView("yudingManaMyQian");
        Users users = (Users)session.getAttribute("user");
        if(null!=users) {
            req.setUsersid(users.getId());
            return getModelAndView(req, pageNo, mav, "id");
        }
        return mav;
    }


    //前台-座位添加预定页面
    @GetMapping("/zuoweiyudingAddQian/{zuoweiid}")
    @ResponseBody
    public ModelAndView zuoweiyudingAddQian(@PathVariable Integer zuoweiid){
        ModelAndView mav = getZuoweiYudingAddModelAndView(zuoweiid,"zuoweiyudingAddQian");
        return mav;
    }

    //后台-座位添加预定页面
    @GetMapping("/zuoweiyudingAdd/{zuoweiid}")
    @ResponseBody
    public ModelAndView zuoweiyudingAdd(@PathVariable Integer zuoweiid){
        ModelAndView mav = getZuoweiYudingAddModelAndView(zuoweiid,"zuoweiyudingAdd");
        return mav;
    }

    //后台-座位预定查看页面
    @RequestMapping("/zuoweiyudingGetByzuoweiid/{zuoweiid}")
    @ResponseBody
    public ModelAndView zuoweiyudingGetByzuoweiid(Yuding req, @PathVariable Integer zuoweiid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("zuoweiyudingChakan");
        req.setZuoweiid(zuoweiid);
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-座位预定查看页面
    @RequestMapping("/zuoweiyudingGetByzuoweiidQian/{zuoweiid}")
    @ResponseBody
    public ModelAndView zuoweiyudingGetByzuoweiidQian(Yuding req, @PathVariable Integer zuoweiid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingManaQian");
            mav.addObject("actionName","yudingManaQian");
        req.setZuoweiid(zuoweiid);
        return getModelAndView(req, pageNo, mav, "id");
    }


    //座位预定数据封装
    private ModelAndView getZuoweiYudingAddModelAndView(Integer zuoweiid, String viewName) {
        ModelAndView mav = new ModelAndView(viewName);
        Zuowei zuowei=zuoweiService.findById(zuoweiid);
        mav.addObject("zuowei", zuowei);
        List<Fangshi> fangshiList=fangshiService.findAll();
        mav.addObject("fangshiList", fangshiList);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
    }
    //前台-支付方式添加预定页面
    @GetMapping("/fangshiyudingAddQian/{fangshiid}")
    @ResponseBody
    public ModelAndView fangshiyudingAddQian(@PathVariable Integer fangshiid){
        ModelAndView mav = getFangshiYudingAddModelAndView(fangshiid,"fangshiyudingAddQian");
        return mav;
    }

    //后台-支付方式添加预定页面
    @GetMapping("/fangshiyudingAdd/{fangshiid}")
    @ResponseBody
    public ModelAndView fangshiyudingAdd(@PathVariable Integer fangshiid){
        ModelAndView mav = getFangshiYudingAddModelAndView(fangshiid,"fangshiyudingAdd");
        return mav;
    }

    //后台-支付方式预定查看页面
    @RequestMapping("/fangshiyudingGetByfangshiid/{fangshiid}")
    @ResponseBody
    public ModelAndView fangshiyudingGetByfangshiid(Yuding req, @PathVariable Integer fangshiid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("fangshiyudingChakan");
        req.setFangshiid(fangshiid);
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-支付方式预定查看页面
    @RequestMapping("/fangshiyudingGetByfangshiidQian/{fangshiid}")
    @ResponseBody
    public ModelAndView fangshiyudingGetByfangshiidQian(Yuding req, @PathVariable Integer fangshiid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingManaQian");
            mav.addObject("actionName","yudingManaQian");
        req.setFangshiid(fangshiid);
        return getModelAndView(req, pageNo, mav, "id");
    }


    //支付方式预定数据封装
    private ModelAndView getFangshiYudingAddModelAndView(Integer fangshiid, String viewName) {
        ModelAndView mav = new ModelAndView(viewName);
        Fangshi fangshi=fangshiService.findById(fangshiid);
        mav.addObject("fangshi", fangshi);
        List<Zuowei> zuoweiList=zuoweiService.findAll();
        mav.addObject("zuoweiList", zuoweiList);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
    }
    //前台-用户添加预定页面
    @GetMapping("/usersyudingAddQian/{usersid}")
    @ResponseBody
    public ModelAndView usersyudingAddQian(@PathVariable Integer usersid){
        ModelAndView mav = getUsersYudingAddModelAndView(usersid,"usersyudingAddQian");
        return mav;
    }

    //后台-用户添加预定页面
    @GetMapping("/usersyudingAdd/{usersid}")
    @ResponseBody
    public ModelAndView usersyudingAdd(@PathVariable Integer usersid){
        ModelAndView mav = getUsersYudingAddModelAndView(usersid,"usersyudingAdd");
        return mav;
    }

    //后台-用户预定查看页面
    @RequestMapping("/usersyudingGetByusersid/{usersid}")
    @ResponseBody
    public ModelAndView usersyudingGetByusersid(Yuding req, @PathVariable Integer usersid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("usersyudingChakan");
        req.setUsersid(usersid);
        return getModelAndView(req, pageNo, mav, "id");
    }

    //前台-用户预定查看页面
    @RequestMapping("/usersyudingGetByusersidQian/{usersid}")
    @ResponseBody
    public ModelAndView usersyudingGetByusersidQian(Yuding req, @PathVariable Integer usersid, @RequestParam(defaultValue = "1",value = "pageNo")int pageNo){
        ModelAndView mav = new ModelAndView("yudingManaQian");
            mav.addObject("actionName","yudingManaQian");
        req.setUsersid(usersid);
        return getModelAndView(req, pageNo, mav, "id");
    }


    //用户预定数据封装
    private ModelAndView getUsersYudingAddModelAndView(Integer usersid, String viewName) {
        ModelAndView mav = new ModelAndView(viewName);
        Users users=usersService.findById(usersid);
        mav.addObject("users", users);
        List<Zuowei> zuoweiList=zuoweiService.findAll();
        mav.addObject("zuoweiList", zuoweiList);
        List<Fangshi> fangshiList=fangshiService.findAll();
        mav.addObject("fangshiList", fangshiList);

        return mav;
    }



    //预定模糊匹配查询分页数据封装
    private ModelAndView getModelAndView(Yuding req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Yuding> pageWrap=new PageWrap<Yuding>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Yuding> pageInfo= yudingService.findPage(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Zuowei> zuoweiList=zuoweiService.findAll();
        mav.addObject("zuoweiList", zuoweiList);
        List<Fangshi> fangshiList=fangshiService.findAll();
        mav.addObject("fangshiList", fangshiList);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
    }

    //预定完全匹配查询分页数据封装
    private ModelAndView getModelAndViewByEqualTo(Yuding req, int pageNo, ModelAndView mav, String sortProperty) {
        PageWrap<Yuding> pageWrap=new PageWrap<Yuding>();
        pageWrap.setModel(req);
        pageWrap.setPageNum(pageNo);
        //设置每页最多几条数据，默认为8条
        //pageWrap.setPageSize(8);
        PageWrap.SortData sortData=new PageWrap.SortData();
        sortData.setDirection("DESC");
        sortData.setProperty(sortProperty);
        List<PageWrap.SortData> sorts = new ArrayList<>();
        sorts.add(sortData);
        pageWrap.setSorts(sorts);
        PageData<Yuding> pageInfo= yudingService.findPageByEqualTo(pageWrap);
        mav.addObject("pageInfo", pageInfo);


        mav.addObject("req", req);
        List<Zuowei> zuoweiList=zuoweiService.findAll();
        mav.addObject("zuoweiList", zuoweiList);
        List<Fangshi> fangshiList=fangshiService.findAll();
        mav.addObject("fangshiList", fangshiList);
        List<Users> usersList=usersService.findAll();
        mav.addObject("usersList", usersList);

        return mav;
        }
}
